package com.briup.MR.InOutFormat;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFilter;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

/**
 * yarn jar HdfsClientDemo-1.0-SNAPSHOT.jar com.briup.MR.InOutFormat.SeqReadMR -D input=/user/hdfs/seq.txt -D output=/user/hdfs/seq_result
 */
public class SeqReadMR extends Configured implements Tool {
    public int run(String[] strings) throws Exception {
        Configuration conf = getConf();
        String input = conf.get("input");
        String output = conf.get("output");

        Job job = Job.getInstance(conf);
        job.setJarByClass(this.getClass());
        job.setJobName("SeqReadMR");

        /*
        采用默认的Map和reduce的时候，输入的键值默认
        是：键LongWritable,值Text
        读取键值类型和默认类型不一致，需要构建job作业设置
        需要的键值类型
         */
        job.setMapOutputKeyClass(IntWritable.class);
        //SequenceFileInputFormat读取SequenceFile格式的文件
        //进入map的键值由SequenceFile格式中的Map决定
        job.setInputFormatClass(SequenceFileInputFilter.class);
        SequenceFileInputFilter.addInputPath(job,new Path(input));
        TextOutputFormat.setOutputPath(job,new Path(output));
        return job.waitForCompletion(true)?0:-1;
    }
    public static void main(String[] args) throws Exception {
        System.exit(new ToolRunner().run(new SeqReadMR(),args));
    }
}
